package com.ypinkj.app.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ypinkj.app.annotation.UserLoginToken;
import com.ypinkj.app.entity.Dict;
import com.ypinkj.app.entity.User;
import com.ypinkj.app.mapper.DictDao;
import com.ypinkj.app.utils.resultutils.Result;
import com.ypinkj.app.utils.resultutils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = "字典管理")
@RestController
@RequestMapping("/dicts")
public class DictController{

	@Autowired
	private DictDao dictDao;

	@UserLoginToken
	@PostMapping
	@ApiOperation(value = "保存")
	public Result save(Dict dict) {
		if (StringUtils.isNotBlank(dict.getK())==true && StringUtils.isNotBlank(dict.getType())==true && StringUtils.isNotBlank(dict.getVal())==true){
			LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(Dict::getK,dict.getK())
						.eq(Dict::getType,dict.getType())
						.eq(Dict::getVal,dict.getVal());
			Integer integer = dictDao.selectCount(queryWrapper);
			if (integer > 0) {
				return ResultUtil.error(500,"数据重复");
			}
			dictDao.insert(dict);
			return ResultUtil.success();
		}
		return ResultUtil.errorMsg();
	}

	/*@GetMapping("/{id}")
	@ApiOperation(value = "根据id获取")
	public Result get(@PathVariable String id) {
		Dict dict = dictDao.selectById(id);
		return ResultUtil.success(dict);
	}*/

	@UserLoginToken
	@PutMapping
	@ApiOperation(value = "修改")
	public Result update(Dict dict) {
		if (StringUtils.isNotBlank(dict.getK()) && StringUtils.isNotBlank(dict.getType())==true && StringUtils.isNotBlank(dict.getVal())==true){
			LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
			queryWrapper.eq(Dict::getK,dict.getK())
					.eq(Dict::getType,dict.getType())
					.eq(Dict::getVal,dict.getVal());
			Integer integer = dictDao.selectCount(queryWrapper);
			if (integer > 0) {
				return ResultUtil.error(500,"数据重复");
			}
			dictDao.updateDict(dict);
			return ResultUtil.success();
		}
		return ResultUtil.errorMsg();
	}

	@UserLoginToken
	@ApiOperation(value = "后台页面分页数据")
	@GetMapping("/getPageList")
	public Object getPageList(int page, int limit, Dict dict){
		//设置分页参数
		PageHelper.startPage(page, limit);
		//查询条件
		LambdaQueryWrapper<Dict> queryWrapper = new LambdaQueryWrapper<>();
		if (dict.getType()!=null&&dict.getType()!=""){
			queryWrapper.eq(Dict::getType,dict.getType());
		}
		if (dict.getK()!=null&&dict.getK()!=""){
			queryWrapper.eq(Dict::getK,dict.getK());
		}
		if (dict.getVal()!=null&&dict.getVal()!=""){
			queryWrapper.eq(Dict::getVal,dict.getVal());
		}
		List selectList = dictDao.selectList(queryWrapper);
		PageInfo<Dict> info = new PageInfo<>(selectList);
		return ResultUtil.successCount(info.getList(),info.getTotal());
	}

	@DeleteMapping("/delete")
	@ApiOperation(value = "删除")
	public Result delete(String id) {
		int i = dictDao.deleteById(id);
		return ResultUtil.success();
	}
	@DeleteMapping("/deleteIds")
	@ApiOperation(value = "多条删除")
	public Result deleteIds(String[] id) {
		dictDao.deleteBatchIds(Arrays.asList(id));
		return ResultUtil.success();
	}

}
